package com.hxzy.controller.system;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.core.domain.entity.SysConfig;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.utils.poi.ExcelUtil;
import com.hxzy.system.domain.dto.SysConfigDTO;
import com.hxzy.system.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

@Api(tags = "参数设置API")
@RestController
@RequestMapping(value = "/api")
public class SysConfigController extends BaseController {

    @Autowired
    private SysConfigService sysConfigService;

    @ApiOperation(value = "参数设置分页查询")
    @GetMapping(value = "/system/config/list")
    public R search(SysConfigDTO sysConfigDTO){
        IPage<SysConfig> page = this.sysConfigService.search(sysConfigDTO);
        return super.pageVO(page);
    }

    /**
     * 新增参数配置
     */
    @ApiOperation(value = "新增参数设置")
    @PostMapping(value = "/system/config")
    public R add(@Validated @RequestBody SysConfig sysConfig) {
        boolean save = this.sysConfigService.save(sysConfig);
        return save? R.ok(): R.build(AckCode.FAIL);
    }

    /**
     * 根据主键查询参数配置
     */
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParam(name = "id",value = "主键")
    @GetMapping(value = "/system/config/{id}")
    public R  findById( @PathVariable(value = "id") Long id){
        SysConfig  sysConfig=this.sysConfigService.findById(id);
        if(Objects.isNull(sysConfig)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        sysConfig.setUpdateTime(null);
        sysConfig.setCreateTime(null);

        return R.okHasData(sysConfig);
    }

    /**
     * 修改参数配置
     */
    @ApiOperation(value = "修改参数设置")
    @PutMapping(value = "/system/update")
    public R update(@Validated @RequestBody SysConfig sysConfig) {
        boolean saved=this.sysConfigService.edit(sysConfig);
        return saved? R.ok(): R.build(AckCode.FAIL);
    }

    /**
     * 删除参数配置
     */
    @ApiOperation(value = "删除参数设置")
    @DeleteMapping("/system/config/{configIds}")
    public R remove( @PathVariable(value = "configIds") Long[] configIds){
        int i = sysConfigService.deleteConfigByIds(configIds);
        return i!=0? R.ok(): R.build(AckCode.FAIL);

    }

    /**
     * 导出数据
     */
    @PostMapping("/system/config/export")
    public void export(HttpServletResponse response, SysConfigDTO sysConfigDTO)
    {
        List<SysConfig> list = this.sysConfigService.searchExport(sysConfigDTO);
        ExcelUtil<SysConfig> util = new ExcelUtil<SysConfig>(SysConfig.class);
        util.exportExcel(response, list, "参数设置数据");
    }


}
